package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class Teacher{
    private static Connection connection=DBConnection.getDBCourseConnection();
    public static String getPwdById(String tid) {
        String sql="select pwd from teacher where tid=?";
        PreparedStatement preparedStatement= null;
        String pwd=null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,tid);
            ResultSet resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                pwd=resultSet.getString("pwd");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pwd;
    }

    public static String getNameById(String tid){
        String sql="select tname from teacher where tid=?";
        PreparedStatement preparedStatement= null;
        String name=null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,tid);
            ResultSet resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                name=resultSet.getString("tname");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return name;
    }

    public static String getSchoolById(String tid){
        String sql="select school from teacher where tid=?";
        PreparedStatement preparedStatement= null;
        String value=null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,tid);
            ResultSet resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                value=resultSet.getString("school");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return value;
    }

    public static String getMajorById(String tid){
        String sql="select major from teacher where tid=?";
        PreparedStatement preparedStatement= null;
        String value=null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,tid);
            ResultSet resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                value=resultSet.getString("major");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return value;
    }

    public static String getGradeById(String tid){
        String sql="select grade from teacher where tid=?";
        PreparedStatement preparedStatement= null;
        String value=null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,tid);
            ResultSet resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                value=resultSet.getString("grade");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return value;
    }

    public static String getDegreeById(String tid){
        String sql="select degree from teacher where tid=?";
        PreparedStatement preparedStatement= null;
        String value=null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,tid);
            ResultSet resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                value=resultSet.getString("degree");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return value;
    }

    public static String getGenderById(String tid){
        String sql="select gender from teacher where tid=?";
        PreparedStatement preparedStatement= null;
        String value=null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,tid);
            ResultSet resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                value=resultSet.getString("gender");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return value;
    }

    public static String getBirthdayById(String tid){
        String sql="select birthday from teacher where tid=?";
        PreparedStatement preparedStatement= null;
        String value=null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,tid);
            ResultSet resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                value=resultSet.getString("birthday");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return value;
    }

    public static boolean hasTid(String tid){
        String sql="select tid from teacher where tid=?";
        PreparedStatement preparedStatement= null;
        boolean result=false;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,tid);
            ResultSet resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                result=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean insert(String tid,String tname,String pwd){
        String sql="insert into teacher(tid,tname,pwd) values(?,?,?)";
        PreparedStatement preparedStatement= null;
        boolean result=false;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,tid);
            preparedStatement.setString(2,tname);
            preparedStatement.setString(3,pwd);
            int i=preparedStatement.executeUpdate();
            if(i==1){
                result=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean updateAllById(String name,String school,String major,String grade,String degree,String gender,String birthday,String headPath,String id){
        String sql="update teacher set tname=?,school=?,major=?,grade=?,degree=?,gender=?,birthday=?,headPath=? where tid=?";
        PreparedStatement preparedStatement=null;
        boolean result=false;
        try {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,school);
            preparedStatement.setString(3,major);
            preparedStatement.setString(4,grade);
            preparedStatement.setString(5,degree);
            preparedStatement.setString(6,gender);
            preparedStatement.setString(7,birthday);
            preparedStatement.setString(8,headPath);
            preparedStatement.setString(9,id);
            int i=preparedStatement.executeUpdate();
            if(i==1){
                result=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getHeadPathById(String id){
        String sql="select headPath from teacher where tid=?";
        PreparedStatement preparedStatement= null;
        String value=null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,id);
            ResultSet resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                value=resultSet.getString("headPath");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return value;
    }



}
